package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(timeout = 500000)
public class RecommendUserApiImpl implements RecommendUserApi {

    // 通过mongoTemplate实现mongo数据库的访问。需要在yml中配置连接的mongo url
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryWithMaxScore(Long userId) {
        //1、构造查询条件: 根据userId查询
        // db.recommend_user.find({userId:1}).sort({score:-1}).limit(1)
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        // 根据score降序排序
        query.with(Sort.by(Sort.Order.desc("score")));
        // 只返回第一条数据
        query.limit(1);

        //2、根据条件查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setRecommendUserId(10L);
            recommendUser.setScore(70D);
        }
        return recommendUser;
    }

    @Override
    public PageResult queryRecommendation(Long userId, Integer page, Integer pagesize) {
        //1、构造查询条件: 根据userId查询
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        // 根据score降序排序
        query.with(Sort.by(Sort.Order.desc("score")));
        // 只返回第一条数据
        query.limit(pagesize).skip((page-1)*pagesize);

        //2、分页查询当前页数据
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        //3、查询总记录数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //4、返回结果
        return new PageResult(page,pagesize, (int) count,list);
    }

    @Override
    public long queryScore(Long userId, Long recommendUserId) {
        // 数据库查询：db.recommend_user.find({userId:1,recommendUserId:57})
        // 构造条件
        Query query = Query.query(
                Criteria.where("userId").is(userId)
                .and("recommendUserId").is(recommendUserId)
        );
        // 查询缘分值
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        // 没有查询到，给一个默认值
        if (recommendUser == null){
            return 70L;
        }
        return recommendUser.getScore().longValue();
    }

    @Override
    public List<RecommendUser> tanhuaRecommendation(Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        // 根据score降序排序
        query.with(Sort.by(Sort.Order.desc("score")));

        //2、分页查询当前页数据
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);

        return list;
    }
}
